#include <iostream>
#include <string.h>
using namespace std;
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    int pipeArr[2];
    int n = pipe(pipeArr);
    if(n < 0) return 1;
    pid_t id = fork();
    if(id == 0)
    {
        close(pipeArr[1]);
        char buff[1024];
        ssize_t ret = 0;
        while(ret = read(pipeArr[0], buff, sizeof(buff) - 1))
        {
            cout << "child get a message : " << buff ;
        }
        exit(0);
        //child
    }
    //father
    close(pipeArr[0]);
    int cnt = 10;
    const char* message = "I am father";
    char buffer[128];
    while(cnt--)
    {
        snprintf(buffer, sizeof buffer, "message: %s, %d\n", message, cnt);
        write(pipeArr[1], buffer, strlen(buffer));
        sleep(1);
    }
    wait(NULL);
    return 0;
}